echo.init();
FastClick.attach(document.body);
var Datas=null;
	new IScroll(".home",{
			click:true
	});
	var arr=['yunqian','yunzhong','chanqian','chanhou','chengzhang','fanzhi']

	$.ajax({
			url:'js/data.json',
			dataType:'json',
			type:'get',
			success:function(data){
				Datas=data
			}
		})

	$(".container").on('click','a',function(e){
		// console.log(e.target)
		var that = $(this).attr('href');
		var id= $(this).attr('id')

		// $.inArray(id,arr)

		e.preventDefault(); 
		$(that).css({
				transition: 'all .3s',
				transform:'translateX(0)'
			}).siblings().css({
				transform:'translateX(100%)'
		})
		if(e.target.parentNode.nodeName=='NAV'){

			var index = $(this).index();
			$("mark").animate({
				left:index*25+"%"
		})

		}
		inTo($(this));

		if(that=='#list'){
			getId(id)
		}else if(that=='#live'){
			getContent($(this));
			// console.log($(this))
		}

	})

	echo.init({
		offset:0,
		throttle:2000
	})

	function getId(id){
		if($.inArray(id,arr) > -1){
			getLoad(id);
		}
	}

	function getLoad(id){
		var str='';
		$.each(Datas[id]['fenlei'],function(index,val){
			// console.log(val)
			str+=`<a href="#live" data_content="${id+'_'+index}"><img src="img/tu/${val.img}" alt=""><span>${val.title}</span></a>`;
			$('.lists').html(str);
		})
		new IScroll(".list",{
			click:true
		});
		
		
	}
	var ls=localStorage;
	if(!ls.getItem("fav")){
		ls.setItem("fav",'[]')
	}


	$('.fav').click(function(){
		// ls.clear()
		// alert(11)
		var tagTitle = $(this).attr("title");
		var arrFav = JSON.parse(ls.getItem("fav"));
		// console.log(tagTitle)
		if($.inArray(tagTitle,arrFav)  > -1){
			alert('已经收藏过了')
		}else{
			//添加
			addFav(tagTitle)
		}
		var str = '';
		var ss = JSON.parse(ls.fav)
		$.each(ss,function(index,val){
			str+='<li>'+val+'</li>'
		})
		$('#favorite').html(str)
		
	})
	function addFav(tagTitle){
		var brrFav = JSON.parse(ls.getItem("fav"));
		brrFav.push( tagTitle  )
		ls.setItem('fav',JSON.stringify(brrFav))
	}

	function inTo(that){
		var href=that.attr("href");
		if(href=='#list'){
			$('.return').show();
			$('.return').attr("href",'#home');
			$('.fav').hide();
			
		}else if(href=='#home'){
			$('.return').hide();
			$('.fav').hide();
			$('h1').text('孕育宝典')
		}else if(href=='#live'){
			$(".fav").attr("title",that.find('span').text())
			$(".fav").show();
			$(".return").attr('href','#list');
		}
	}

	function getContent(el){
		var arr = el.attr('data_content').split('_')
		// console.log(arr)
		// console.log(el.attr('data_content'))
		// console.log(Datas[arr[0]]['fenlei'][arr[1]])
		$('.cc').html(Datas[arr[0]]['fenlei'][arr[1]].content);
		
		$('h1').html(Datas[arr[0]]['fenlei'][arr[1]].title);
		new IScroll(".live",{
			click:true
		});
	}